﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace BusinessComponents.Managers
{
    class RoomMgr : IRoomMgr
    {
        WaterFrontEntities wfe = new WaterFrontEntities();

        public IEnumerable<Room> FindAllRooms()
        {
            return wfe.Rooms;
        }

        public IEnumerable<Room> FindAllActiveRoomsByType(int roomTypeCd)
        {
            using (wfe = new WaterFrontEntities())
            {
                IEnumerable<Room> list = from rm in wfe.Rooms
                                         where rm.TypeCode == roomTypeCd && rm.Status == 1
                                         select rm;
                if (list != null)
                {
                    return list.ToList();
                }
                else
                {
                    return new List<Room>();
                }
            }
        }

        public virtual Room GetRoom(string roomNum)
        {
            return wfe.Rooms.SingleOrDefault(r => r.RoomNo == roomNum);
        }
        public virtual void AddRoom(Room room)
        {
            wfe.Rooms.AddObject(room);
        }
        public virtual void DeleteRoom(Room room)
        {
            wfe.Rooms.DeleteObject(room);
        }
        public virtual void Save()
        {
            wfe.SaveChanges();
        }
        public virtual int GetPrice(string roomNo)
        {
            Room r = this.GetRoom(roomNo);
            if (r == null)
            {
                return 0;
            }
            else
            {
                return (int)r.RoomType.StdPrice;
            }
        }

        public virtual List<Room> SearchRoom(int type, int status, string number)
        {

            IEnumerable<Room> list = from rm in wfe.Rooms
                                     where (rm.TypeCode == type || type == 0) && (rm.Status == status || status == -1) && (rm.RoomNo.LastIndexOf(number) > 0 || number == "")
                                     orderby rm.RoomNo
                                     select rm;
            if (list != null)
            {
                return list.ToList();
            }
            else
            {
                return new List<Room>();
            }
        }
    }
}
